﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using log4net;

namespace Estimate.Common.DataFileFinder
{
    /// <summary>
    /// 数值预报产品文件获取类
    /// </summary>
    public class ForeDataFilesFinder : IDataFileFinder
    {
        public static readonly ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string filterFiles(string dirPath, string[] filepattern)
        {
            if (Directory.Exists(dirPath))
            {
                string[] fileArray = GetFileList(dirPath, filepattern);
                if( fileArray.Length > 0)
                    return fileArray[fileArray.Length - 1]; 
            }
            else
            {
                Logger.Error(string.Format("无法找到文件夹 {0}!", dirPath));
            }
            return "";
        }

        public string[] GetFileFromDirectory(DateTime dt, string dirPath)
        {
            return GetFileFromDirectory(dt, dirPath, "");
        }

        public string[] GetFileFromDirectory(DateTime dt, string dirPath, string fileFilter)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");
            List<string> result = new List<string>();

            //早晨06点预报
            string f = filterFiles(dirPath, new string[] 
                {
                    string.Format(fileFilter,date0,"18",date,"0000"),
                    string.Format(fileFilter,date0,"19",date,"0000"),
                    string.Format(fileFilter,date0,"20",date,"0000")
                    //"Z_SEVP_C_BABJ_" + date0 + "18????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                    // "Z_SEVP_C_BABJ_" + date0 + "19????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT",
                    // "Z_SEVP_C_BABJ_" + date0 + "20????" + "_P_RFFC-SCMOC-" + date + "0000-16812.TXT"
                });
            if (f != "")
                result.Add(f);

            //下午16点预报
            f = filterFiles(dirPath, new string[] 
                {
                    string.Format(fileFilter,date,"06",date,"1200"),
                    string.Format(fileFilter,date,"07",date,"1200")

                    //"Z_SEVP_C_BABJ_" + date + "06????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT",
                    //"Z_SEVP_C_BABJ_" + date + "07????" + "_P_RFFC-SCMOC-" + date + "1200-16812.TXT"            
                });
            if (f != "")
                result.Add(f);

            return result.ToArray();
        }

        public Dictionary<string, string[]> GetFileListFromDirectory(DateTime dt, string dirPath)
        {
            return GetFileListFromDirectory(dt, dirPath, "Z_SEVP_I_BENJ_{0}{1}????_P_RFFC-AVEECWMF-{2}{3}-19203.TXT");
        }

        public Dictionary<string, string[]> GetFileListFromDirectory(DateTime dt, string dirPath, string fileFilter)
        {
            Dictionary<string, string[]> result = new Dictionary<string, string[]>();
            string[] f;
            if (dt == DateTime.MinValue)//日期为最小值时，表示不按日期筛选
            {
                f = GetFileList(dirPath, new string[] { string.Format(fileFilter, "????????", "??", "????????", "????") });
            }
            else
            {
                string date1 = dt.ToString("yyyyMMdd");
                string date2 = dt.AddDays(-1).ToString("yyyyMMdd");

                f = GetFileList(dirPath, new string[] { string.Format(fileFilter, date1, "??", date1, "????"),
                                                       string.Format(fileFilter, date2, "??", date1, "????")});
            }

            result.Add("", f);

            return result;
        }

        private string[] GetFileList(string dirPath, string[] filepattern)
        {
            List<string> files = new List<string>();
            foreach (string pattern in filepattern)
            {
                if (Directory.Exists(dirPath))
                    files.AddRange(Directory.GetFiles(dirPath, pattern));
                else
                    Logger.Error(string.Format("无法找到文件夹 {0}!", dirPath));

            }
            if (files.Count == 0)
                return new string[] { };

            string[] fileArray = files.ToArray();
            //对文件组排序
            for (int i = 0; i < fileArray.Length; i++)
            {
                for (int j = i + 1; j < fileArray.Length; j++)
                {
                    if (files[i].CompareTo(files[j]) > 0)
                    {
                        string temp = files[i];
                        files[i] = files[j];
                        files[j] = temp;
                    }
                }
            }

            return fileArray;
        }

        public string[] GetFileNames(DateTime dt)
        {
            throw new NotImplementedException();
        }
    }
}
